package com.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.house.entity.*;
import com.house.enums.ItemType;
import com.house.exception.LeaseException;
import com.house.mapper.*;
import com.house.result.ResultCodeEnum;
import com.house.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.house.vo.apartment.ApartmentDetailVo;
import com.house.vo.apartment.ApartmentItemVo;
import com.house.vo.apartment.ApartmentQueryVo;
import com.house.vo.apartment.ApartmentSubmitVo;
import com.house.vo.fee.FeeValueVo;
import com.house.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lfz
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {
        boolean b = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (b) {
            //删除公寓图片
            LambdaQueryWrapper<GraphInfo> GraphInfowrapper = new LambdaQueryWrapper();
            GraphInfowrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(GraphInfowrapper);
            //删除公寓设施信息
            LambdaQueryWrapper<ApartmentFacility> FacilityWrapper = new LambdaQueryWrapper<>();
            FacilityWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(FacilityWrapper);
            //删除公寓标签信息
            LambdaQueryWrapper<ApartmentLabel> LabelWrapper = new LambdaQueryWrapper<>();
            LabelWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(LabelWrapper);
            //删除公寓费用信息
            LambdaQueryWrapper<ApartmentFeeValue> FeeValueWrapper = new LambdaQueryWrapper<>();
            FeeValueWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(FeeValueWrapper);
        }
        //插入图片
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getGraphVoList())) {
            for (GraphVo graphVo : apartmentSubmitVo.getGraphVoList()) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //插入设施到公寓设施表
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getFacilityInfoIds())) {
            for (Long facilityInfoId : apartmentSubmitVo.getFacilityInfoIds()) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityInfoId);
                apartmentFacilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }
//插入标签到公寓标签表
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getLabelIds())) {
            for (Long labelId : apartmentSubmitVo.getLabelIds()) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }
        //插入杂费到公寓杂费表
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getFeeValueIds())) {
            for (Long feeValueId : apartmentSubmitVo.getFeeValueIds()) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
    }

    @Override
    public IPage<ApartmentItemVo> selectbypage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.selectbypage(page, queryVo);

    }

    @Override
    public ApartmentDetailVo selectbyapartmentId(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
//        查询公寓图片信息
        List<GraphVo> list = graphInfoMapper.selectbyapartmentid(ItemType.APARTMENT, id);
//        查询公寓标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectbyapartmentidandtype(ItemType.APARTMENT, id);
//        查询公寓设施信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectbyapartmentidandtype(ItemType.APARTMENT, id);
//        查询公寓杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectbyapartmentidandtype(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(list);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    @Override
    public void removeByapartmentId(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoWrapper = new LambdaQueryWrapper<>();
        roomInfoWrapper.eq(id!=null,RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoWrapper);
        if (count>0){
//            公寓存在未处理的房间，无法删除
            throw new LeaseException(ResultCodeEnum.ADMIN_Apartment_Deleted_Erroe.getMessage(),ResultCodeEnum.ADMIN_Apartment_Deleted_Erroe.getCode());
        }
        super.removeById(id);
        //删除公寓图片
        LambdaQueryWrapper<GraphInfo> GraphInfowrapper = new LambdaQueryWrapper();
        GraphInfowrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, id);
        graphInfoService.remove(GraphInfowrapper);
        //删除公寓设施信息
        LambdaQueryWrapper<ApartmentFacility> FacilityWrapper = new LambdaQueryWrapper<>();
        FacilityWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(FacilityWrapper);
        //删除公寓标签信息
        LambdaQueryWrapper<ApartmentLabel> LabelWrapper = new LambdaQueryWrapper<>();
        LabelWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(LabelWrapper);
        //删除公寓费用信息
        LambdaQueryWrapper<ApartmentFeeValue> FeeValueWrapper = new LambdaQueryWrapper<>();
        FeeValueWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(FeeValueWrapper);
    }


}




